package com.zimmur.app.mocolink.manage.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zimmur.app.mocolink.domain.form.UserMoneyForm;
import com.zimmur.app.mocolink.domain.model.UserGrade;
import com.zimmur.app.mocolink.domain.model.UserMoneyLog;
import com.zimmur.app.mocolink.domain.param.AppUserParam;
import com.zimmur.app.mocolink.domain.param.UserMoneyLogParam;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.domain.vo.AppUserInfo;
import com.zimmur.app.mocolink.domain.vo.AppUserInfoForManage;
import com.zimmur.app.mocolink.manage.web.common.ReturnJsonEntity;
import com.zimmur.app.mocolink.manage.web.common.StatusCodeEnum;
import com.zimmur.app.mocolink.service.IAppUserService;
import com.zimmur.app.mocolink.service.IUserGradeService;
import com.zimmur.app.mocolink.service.IUserMoneyLogService;

@Controller
@RequestMapping("/appUser")
public class AppUserController extends BaseController{
	private static final Logger log = LoggerFactory.getLogger(AppUserController.class);
	@Autowired
	private IAppUserService appUserService;
	@Autowired
	private IUserGradeService userGradeService;
	@Autowired
	private IUserMoneyLogService userMoneyLogService;
	
	@RequestMapping("/index")
	public String index(ModelMap model){
		AppUserParam param=new AppUserParam();
		param.setPageNo(1);
		List<UserGrade> gradeList = userGradeService.selectAll();
		PageUtil<AppUserInfoForManage> page = appUserService.selectByParam(param);
		model.addAttribute("page",page);
		model.addAttribute("param",param);
		model.addAttribute("gradeList", gradeList);
		return "/center/appUser/list";
	}
	
	@RequestMapping("/query")
	public String query(ModelMap model){
		Integer pageNo = getParam("pageNo", 1);
		String keyword = getParam("keyword");
		Integer gradeId = getParam("gradeId", 0);
		AppUserParam param=new AppUserParam();
		param.setPageNo(pageNo);
		if(!StringUtils.isBlank(keyword)){
			param.setKeyword(keyword);
		}
		if(gradeId>0){
			param.setGradeId(gradeId);
		}
		List<UserGrade> gradeList = userGradeService.selectAll();
		PageUtil<AppUserInfoForManage> page = appUserService.selectByParam(param);
		model.addAttribute("gradeList", gradeList);
		model.addAttribute("page",page);
		model.addAttribute("param",param);
		return "/center/appUser/list";
	}
	@RequestMapping("/queryMoneyLog")
	public String queryMoneyLog(ModelMap model){
		Integer pageNo = getParam("pageNo", 1);
		Integer userId = getParam("userId", 0);
		UserMoneyLogParam param = new UserMoneyLogParam();
		param.setPageNo(pageNo);
		param.setUserId(userId);
		PageUtil<UserMoneyLog> page = userMoneyLogService.selectByParam(param);
		model.addAttribute("page",page);
		model.addAttribute("userId",userId);
		return "/center/appUser/moneyLog";
	}
	@RequestMapping("/editMoney")
	public String editMoney(ModelMap model){
		Integer appUserId = getParam("userId", 0);
		Integer type = getParam("type", 0);
		AppUserInfo appUserInfo = appUserService.selectInfoByUserId(appUserId);
		model.addAttribute("appUser",appUserInfo);
		model.addAttribute("type",type);
		return "/center/appUser/editMoney";
	}
	
	@ResponseBody
	@RequestMapping("/saveMoney")
	public ReturnJsonEntity saveMoney(UserMoneyForm form){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		String adminName = getUsername();
		form.setAdminName(adminName);
		if(form.getType()!=null && form.getAppUserId()!=null && form.getMoney()!=null){
			int flag = appUserService.updateMoney(form);
			if(flag>0){
				codeEnum = StatusCodeEnum.CODE000000;
			}else if(flag == -1){
				codeEnum = StatusCodeEnum.CODE601403;
			}else if(flag == -2){
				codeEnum = StatusCodeEnum.CODE601404;
			}
		}else{
			codeEnum = StatusCodeEnum.CODE601405;
		}
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/saveGrade")
	public ReturnJsonEntity saveGrade(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		String userIds = getParam("userIds");
		Integer gradeId = getParam("gradeId",0);
		if(StringUtils.isBlank(userIds)){
			codeEnum = StatusCodeEnum.CODE601401;
		}else if(gradeId <= 0){
			codeEnum = StatusCodeEnum.CODE601402;
		}else{
			int flag = appUserService.batchUpdateUserGrade(userIds, gradeId);
			if(flag>0){
				codeEnum=StatusCodeEnum.CODE000000;
			}
		}
		entity.init(codeEnum);
		return entity;
	}
	
}
